package com.jiuyin.dianjing.ui.activity.welcome;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.activity.login.LoginActivity;
import com.jiuyin.dianjing.ui.activity.main.MainActivity;
import com.jiuyin.dianjing.view.WelcomeVideoView;


public class WelcomeActivity extends BaseActivity {
    private WelcomeVideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        if (HelperApplication.isFirstLaunch()) {
            HelperApplication.setLaunched();
        } else {
            goTargetActivity();
        }
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doRequestPermissions() {
    }

    private void goTargetActivity() {
        String token = HelperApplication.getToken();
        if (!TextUtils.isEmpty(token)) {
            goTargetActivity(MainActivity.class);
        } else {
            goTargetActivity(LoginActivity.class);
        }
        finish();
    }

    @Override
    protected void initView() {
        mVideoView = findViewById(R.id.videoview);
        TextView skipView = findViewById(R.id.tv_skip);
        skipView.setOnClickListener(v -> {
            goTargetActivity();
        });
        mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.welcome));
        mVideoView.start();
        mVideoView.setOnCompletionListener(mediaPlayer -> goTargetActivity());
    }

    /**
     * 屏蔽返回键
     */
    @Override
    public void onBackPressed() {
    }

}
